package com.edevp.modules.user.web;

import com.edevp.common.core.dto.R;
import com.edevp.modules.user.config.UserProperties;
import com.edevp.modules.user.dto.UserInfoDTO;
import com.edevp.modules.user.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author lean
 */
@RestController
@RequestMapping("/users")
@AllArgsConstructor
public class UserController {

    private final UserProperties properties;
    private final UserService userService;

    @GetMapping("/ok")
    public String ok() {
        return properties.getValue();
    }


    @GetMapping("/info/{id}")
    public R<UserInfoDTO> get(HttpServletRequest request, @PathVariable("id") String id, @RequestHeader(value = "X-Request-red", required = false) String header) throws InterruptedException {
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String value = request.getHeader(name);
                // System.out.println("name:"+name+", value:"+value);
            }
        }
        UserInfoDTO data = userService.queryById(id);
        data.setName(properties.getValue()+"-"+id+"-"+(header == null?"":header));
        return R.ok(data);
    }
}
